﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Jzy.BLL.DocManger;


using Jzy.Model.DocManger;
using Frame.Lib.Utilities;
using Frame.Lib.Common;
using Frame.Lib.Core.Right;
using Frame.Lib.Core.Dept;
using Frame.Lib.Enums;
using Frame.Lib.Core.Attach;
using Jzy.DAL;


namespace Jzy.Web.DocManager.Document
{
    public partial class Edit : PageEdit
    {
        private DocumentBO _doc;
        private DocumentBO Doc
        {
            get
            {
                if (_doc == null)
                {
                    _doc = new DocumentBO();
                }
                return _doc;
            }
        }
        private string DirID
        {
            get
            {
                return Request.QueryString["DirID"];
            }
        }

        protected string Content
        {
            get;
            set;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            JSUtil.SetDivHeight(pnlMain.ClientID, 32);

            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(PKID))
                {
                    DispInfo();
                }
                else
                {
                    hidDirIDs.Value = DirID;
                    txtDirName.Text = (new DirBO()).GetName(DirID);                   
                    trImages.Visible = false;
                }
            }
            AddAttr();
            this.EnableUpload();
        }

        private void AddAttr()
        {
            JSUtil.ShowTreeDlg(hidDirIDs, txtDirName, "DocDir", "选择目录", true);
            JSUtil.ClickBtn(lnkSave, btnSave);
        }

        private void DispInfo()
        {
            DocumentInfo info = this.Doc.GetInfo(this.PKID);
            EntityUtil.SetControl(pnlMain, info);
            hidDirIDs.Value = info.DirID.ToString();
            txtDirName.Text = (new DirBO()).GetInfo(info.DirID).Name;


            this.Content = info.Content.Replace("'","\\'").Replace("\r\n","");

           
            rptAttach.DataSource = info.Attachs;
            rptAttach.DataBind();

            rptPicture.DataSource = info.Pictures;
            rptPicture.DataBind();

            if (info.Pictures.Count > 0)
            {
                trImages.Visible = true;
            }
            else
            {
                trImages.Visible = false;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

           
            DocumentInfo info = Doc.GetInfo(PKID);

            EntityUtil.SetEntity(pnlMain, info);
            info.DirID = int.Parse(hidDirIDs.Value);

            if (string.IsNullOrEmpty(PKID))
            {
                info.InputTime = DateTime.Now;
            }
            info.CreatorID = AppSession.UserID;
            info.CreatorName = AppSession.UserName;


            info.Content = Request.Form["Content"];
            info.Orders = this.Doc.GetMaxOrder();

            this.Doc.Save(info);

            //增加默认权限(浏览)
            if (this.PKID == null)
            {
                int bindID = (int)info.ID;
                RecordRightBO right = new RecordRightBO(OTableName.DocRight);
                DeptBO dept = new DeptBO();
                int rootID = (int)dept.GetRootID();
                List<int> lstDeptIDs = new List<int>();
                lstDeptIDs.Add(rootID);

                right.SaveOwnerIDs(lstDeptIDs, bindID, RecordRightName.Browse, RightOwnerType.Dept);
                right.SaveOwnerIDs(new List<int>(new int[] { AppSession.UserID }), bindID, RecordRightName.Creator, RightOwnerType.User);
            }

            UserImage userImage = new UserImage(OTableName.DocPicture);

            ImageBO picture = new ImageBO(OTableName.DocPicture);
            AttachBO attach = new AttachBO(OTableName.DocAttach);

            foreach (string key in Request.Files.Keys)
            {

                if (key.Contains("pic"))
                {
                    //判断图片格式
                    string extention = System.IO.Path.GetExtension(Request.Files[key].FileName).ToLower();
                    if (extention == ".jpg" || extention == ".png" || extention == ".gif" || extention == ".bmp")
                    {
                        System.Drawing.Bitmap img = new System.Drawing.Bitmap(Request.Files[key].InputStream);
                        ImageInfo pictureInfo = new ImageInfo();
                        pictureInfo.Name = System.IO.Path.GetFileName(Request.Files[key].FileName);
                        pictureInfo.BindID = info.ID;
                        pictureInfo.InputTime = DateTime.Now;
                        pictureInfo.Width = img.Width;
                        pictureInfo.Height = img.Height;
                        pictureInfo.Size = (int)Request.Files[key].InputStream.Length;
                        pictureInfo.Orders = picture.GetMaxOrder(pictureInfo.BindID);

                        this.Doc.SaveImage(pictureInfo, Request.Files[key], info.DirInfo);

                        picture.Save(pictureInfo);
                    }
                }
                else if (key.Contains("attach"))
                {
                    attach.Save(Request.Files[key], info.ID);
                }
            }

            Response.Redirect("List.aspx?DirID=" + this.Encrypt(info.DirID));
        }

        protected void rptAttach_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            AttachBO attach = new AttachBO(OTableName.DocAttach);
            attach.Delete(Convert.ToInt32(e.CommandArgument));
            DispInfo();
        }

        protected void rptPicture_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            ImageBO picture = new ImageBO(OTableName.DocPicture);
            picture.Delete(Convert.ToInt32(e.CommandArgument));
            DispInfo();
        }
        
    }
}
